package com.github.zhangkaitao.shiro.chapter16.web.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
public class LoginController {

    /*
     * @Description: 登录提交POST方法
     *       @param: req, model
     *      @return: String
     */
    @RequestMapping(value = "/login"    )
    public String showLoginForm(HttpServletRequest req, Model model) {

        // 请求.获取属性？参数？（shiroLoginFailure是从哪来的？）
        String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        String error = null;

        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }

        model.addAttribute("error", error);
        return "login";
    }


}
